<?php
namespace Home\Controller;
use Think\Controller;

class LoginController extends CommonController {

	public function ajaxDoLogin() {
		$user_tel = I('user_tel', null);
		$pwd      = I('pwd', null);
		$state    = I('state', null);
		$sms_code = I('sms_code', null);
		$repwd    = I('repwd', null);
		if (empty($user_tel) || empty($pwd) || empty($state)) {
			$response['msg'] = '参数异常';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		//判断状态
		if ($state == 'login') {
			$userSrv   = D('UserInfo');
			$user_info = $userSrv->where("user_tel = '" . $user_tel . "'")->find();
			if (empty($user_info)) {
				$response['msg'] = '没有该账户';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);
			}
			if ($pwd != $user_info['password']) {
				$response['msg'] = '密码不正确';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);
			}
			$this->doLogin($user_info['user_id']);
			$updateData['last_ip']         = get_client_ip();
			$updateData['last_login_date'] = date("Y-m-d H:i:s");
			$userSrv->where("user_id = '" . $user_info['user_id'] . "'")->save($updateData);
			if(!empty($_SESSION['wechat_openid'])){
				$wechatUserInfo = D('WechatUserInfo');
				$wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->save(array('user_id'=>$user_info['user_id']));
			}
			$response['msg'] = '登陆成功';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		} elseif($state == 'register') {
			if ($sms_code != $_SESSION['sms_code']) {
				$response['rsc'] = C('RESPONSE.FAILED');
				$response['msg'] = '验证码错误';
				$this->ajaxReturn($response);
			}
			if ($user_tel != $_SESSION['reciv_mobile']) {
				$response['rsc'] = C('RESPONSE.FAILED');
				$response['msg'] = '错误的手机号';
				$this->ajaxReturn($response);
			}
			$userSrv   = D('UserInfo');
			$user_info = $userSrv->where("user_tel = '" . $user_tel . "'")->find();
			if (!empty($user_info)) {
				$response['msg'] = '该手机号已经注册';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);
			}			
			$data['user_id']         = new_doc_id('USER');
			$data['password']        = $pwd;
			$data['user_type']       = 0;
			$data['nick_name']       = $data['user_id'];
			$data['user_tel']        = $user_tel;
			$data['register_from']   = 'APP';
			$data['last_ip']         = get_client_ip();
			$data['last_login_date'] = date("Y-m-d H:i:s");
			$dbRes                   = $userSrv->data($data)->add();
			if ($dbRes === false) {
				$response['rsc'] = C('RESPONSE.FAILED');
				$response['msg'] = '注册失败';
				$this->ajaxReturn($response);
			}
			$this->doLogin($data['user_id']);
			if(!empty($_SESSION['wechat_openid'])){
				$wechatUserInfo = D('WechatUserInfo');
				$wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->save(array('user_id'=>$user_info['user_id']));
			}
			$response['msg'] = '登陆成功';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		} else if($state == 'findpwd') {
			if ($pwd != $repwd) {
				$response['msg'] = '两次密码输入不一致';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);		
			}
			if ($sms_code != $_SESSION['sms_code']) {
				$response['rsc'] = C('RESPONSE.FAILED');
				$response['msg'] = '验证码错误';
				$this->ajaxReturn($response);
			}
			if ($user_tel != $_SESSION['reciv_mobile']) {
				$response['rsc'] = C('RESPONSE.FAILED');
				$response['msg'] = '错误的手机号';
				$this->ajaxReturn($response);
			}
			$userSrv = D('UserInfo');
			$userSrv->where("user_tel = '".$user_tel."'")->save(array('password'=>$pwd));
			$response['msg'] = '修改成功';
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['state'] = 'findpwd';
			$this->ajaxReturn($response);
		} else {
				$response['msg'] = '参数异常';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);			
		}
	}

	public function ajaxQuickLogin() {
		$mobile   = I('mobile', '');
		$sms_code = I('sms_code', '');
		if ($mobile != $_SESSION['reciv_mobile']) {
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '错误的手机号';
			$this->ajaxReturn($response);
		}
		if ($sms_code != $_SESSION['sms_code']) {
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '验证码错误';
			$this->ajaxReturn($response);
		}
		$userSrv   = D('UserInfo');
		$user_info = $userSrv->where("user_tel = '" . $mobile . "'")->find();
		if (empty($user_info)) {
			$data['user_id']         = new_doc_id('USER');
			$data['password']        = md5($pwd);
			$data['user_type']       = 0;
			$data['nick_name']       = $data['user_id'];
			$data['user_tel']        = $mobile;
			$data['register_from']   = 'APP';
			$data['last_ip']         = get_client_ip();
			$data['last_login_date'] = date("Y-m-d H:i:s");
			$dbRes                   = $userSrv->data($data)->add();
			if ($dbRes === false) {
				$response['rsc'] = C('RESPONSE.FAILED');
				$response['msg'] = '注册失败';
				$this->ajaxReturn($response);
			}
			$this->doLogin($data['user_id']);
			$response['msg'] = '登陆成功';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		} else {
			$this->doLogin($user_info['user_id']);
			$updateData['last_ip']         = get_client_ip();
			$updateData['last_login_date'] = date("Y-m-d H:i:s");
			$userSrv->where("user_id = '" . $user_info['user_id'] . "'")->save($updateData);
			$response['msg'] = '登陆成功';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		}
	}

	public function ajaxCheckLogin() {
		if ($this->checklogin()) {
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$response['msg'] = '已登录';
		} else {
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '尚未登录';
		}
		$this->ajaxReturn($response);
	}

	public function ajaxLogout() {
		$this->doLogout();
		$response['msg'] = '退出成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	public function ajaxSetPwdForFind(){
		$user_tel = I('user_tel',null);
		$new_pwd = I('new_pwd',null);
		$re_new_pwd = I('re_new_pwd',null);
		$sms_code = I('sms_code',null);
		if(empty($user_tel)||empty($new_pwd)||empty($re_new_pwd)||empty($sms_code)){
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '参数异常';
			$this->ajaxReturn($response);
		}
		if($new_pwd!=$re_new_pwd){
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '两次密码输入不一致';
			$this->ajaxReturn($response);
		}
		if($user_tel!=I('session.reciv_mobile')){
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '参数异常';
			$this->ajaxReturn($response);
		}
		if($sms_code!=I('session.sms_code')){
			$response['rsc'] = C('RESPONSE.FAILED');
			$response['msg'] = '短信验证码错误';
			$this->ajaxReturn($response);
		}
		$userSrv = D('UserInfo');
		$userSrv->where("user_tel = '".$user_tel."'")->save(array('password'=>$new_pwd));
		$response['msg'] = '成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}

}